package com.loongtech.bi.manager.player;

import com.loongtech.bi.entity.player.EntityActiveDevice;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActiveDeviceManager extends ManagerQueryCacheBase<EntityActiveDevice> {
	private static final long serialVersionUID = 1L;

	/**
	 * 根据开始结束时间查询所有激活设备数据
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<EntityActiveDevice> getActiveDevicesByDate(Date dateStart, Date dateEnd) {
		return findByQuery("from EntityActiveDevice where 1=1 and day>=:dayStart and day<=:dayEnd order by day ", "dayStart", dateStart, "dayEnd", dateEnd);
	}

    /**
     * 根据开始结束时间查询所有激活设备数据
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<EntityActiveDevice> getActiveDevicesByDate2(Date dateStart, Date dateEnd) {
        return findByQuery("from EntityActiveDevice where 1=1 and day>=:dayStart and day<=:dayEnd and serverid='0' order by day ", "dayStart", dateStart, "dayEnd", dateEnd);
    }

    /**
     * 根据开始结束时间查询从开服到现在全服激活设备数据
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public long getActiveDevicesFromOpenDate(Date dateStart, Date dateEnd) {
        Long ss = getByQuery("select sum(active_device) from EntityActiveDevice where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '0' and channelid = '0' and serverid = '0'", "dayStart", dateStart, "dayEnd", dateEnd);
        return ss == null ? 0 : ss;
    }

    public List<Object[]> getActiveDevicesFromOpenDate2(Date dateStart, Date dateEnd) {
        return findByQuery("select platformid, sum(active_device) from EntityActiveDevice where 1=1 and day>=:dayStart and day<=:dayEnd and channelid = '0' and serverid = '0' group by platformid ", "dayStart", dateStart, "dayEnd", dateEnd);
    }

    /**
     * 根据开始结束时间查询从开服到现在全服激活设备数据（分平台）
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public long getActiveDevicesFromOpenDateByPlatform(Date dateStart, Date dateEnd, String platformid) {
        Long ss = getByQuery("select sum(active_device) from EntityActiveDevice where 1=1 and day>=:dayStart and day<=:dayEnd and platformid =:platformid ", "dayStart", dateStart, "dayEnd", dateEnd, "platformid",platformid);
        return ss == null ? 0 : ss;
    }

	/**
	 * 根据开始结束时间查询某个维度的数值
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getActiveDeviceByAllKind(Date dateStart, Date dateEnd,String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),active_device from EntityActiveDevice where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' and active_device is not null group by day order by day desc", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 根据platformid 查询
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getActiveDeviceGroupByPlatform(Date dateStart, Date dateEnd) {
		return findByQuery("select platformid,sum(active_device) from EntityActiveDevice where day>=:dayStart and day<=:dayEnd and platformid != '0' and active_device is not null group by platformid", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 根据channelid 查询
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getActiveDeviceGroupByChannel(Date dateStart, Date dateEnd) {
		return findByQuery("select channelid,sum(active_device) from EntityActiveDevice where day>=:dayStart and day<=:dayEnd and channelid != '0' and active_device is not null group by channelid", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 根据serverid 查询
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getActiveDeviceGroupByServer(Date dateStart, Date dateEnd) {
		return findByQuery("select serverid,sum(active_device) from EntityActiveDevice where day>=:dayStart and day<=:dayEnd and serverid != '0' and active_device is not null group by serverid", "dayStart", dateStart, "dayEnd", dateEnd);
	}
}
